﻿--[[
   @brief    CTips广告(进助手页和游戏退出时，右下角弹窗)
   @author   hanmanhuang
   @version  1.0
   @date     2017-10-19
--]]

CTips_view =
{
    tips_url = LoadStr("CTIPS_INDEX_URL");
    busy = false;
    tick_to_close = false;

    init = function()
        CTips_view.inner_log("init");
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[CTips_view] %s", msg), core_msg);
    end;

    ShowCTips = function(tips_info)
        if CTips_view.busy then
            CTips_view.inner_log("pre tips is showing, not finish!");
        end;

        if CTips_view.Exists() == false then
            CTips_view.CreateDlg();
            CTips_view.MoveInitPos();
        else
            CTips_view.Hide();
            CTips_view.MoveInitPos();
        end;

        CTips_view.busy = true;
        CTips_control.tips_info_list[CTips_control.cur_tips_id] = nil;
        CTips_control.cur_tips_id = CTips_control.cur_tips_id + 1;
        CTips_control.tips_info_list[CTips_control.cur_tips_id] = tips_info;
        CTips_view.Navigate(CTips_control.cur_tips_id);
    end;

    Exists = function()
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_CTIPS_DLG);
        if component == 0 or component == nil then
            return false;
        end;
        return true;
    end;
    
    CreateDlg = function()
        layout_mgr.restore_layout("CTips.tpf");
    end;

    MoveInitPos = function()
        local tips_mgr = base_utility.get_component_adapter("ITIPS_mgr",
            "ierd_tgp.tips_mgr.GetTipsMgrModule();");
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_CTIPS_DLG);
        if tips_mgr == nil or component == nil then
            return;
        end
    
        local wnd_rect = component:GetRectEx(TRUE,TRUE);
        local wnd_width = wnd_rect.right - wnd_rect.left;
        local wnd_height = wnd_rect.bottom - wnd_rect.top;

        
        local resolution_x, resolution_y, work_area_x, work_area_y = 
            ui_utility.get_screen_resolution();
        
        local new_pos = { x = work_area_x - wnd_width - 5,
                          y = work_area_y; }
    
        local new_rect = MainCommon.move_rect(wnd_rect, new_pos);
        component:MoveEx(new_rect,TRUE);
    end;

    Navigate = function(id)
        local tgpid = "0";
        if nil ~= wegame_mainframe_model and 0 ~= wegame_mainframe_model.self_tgp_id then
            tgpid = tostring(wegame_mainframe_model.self_tgp_id);
        end;
        local url = CTips_view.tips_url.."?tipsid="..tostring(id).."&tgpid="..tgpid;
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_CTIPS_BROWSER);
        local const_ui_str, str_holder = U8toCUIStr(url);

        CTips_view.inner_log("Navigate : "..url);
        if component ~= nil then
            component:Navigate(const_ui_str);
        else
            CTips_view.busy = false;
        end;
    end;

    Show = function(show)
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_CTIPS_DLG);
        if component ~= nil then
            if show then
                component:Show(TRUE);
            else 
                component:Show(FALSE);
            end;
        end;
    end;

    Hide = function()
        CTips_view.inner_log("Hide");
        CTips_view.Show(false);
        CTips_view.busy = false;
        CTips_view.RemoveTick();
    end;

    Close = function()
        CTips_view.inner_log("Close");
        CTips_view.Hide();
        local uilib = ui_utility.uilib();
        local control_id = uilib:GetControlIDbyCustomID(CID_CTIPS_DLG);
        if nil ~= control_id or Tenio.INVALID_CONTROLID ~= control_id then
            layout_mgr.delay_remove_ctrl(control_id);
        end;
    end;

    -- 开启30s定时器
    StartTick = function()
        if CTips_view.tick_to_close == false then
            CTips_view.inner_log("StartTick");
            local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
            tpfui:AddInterval("CTips_view.TickCount", 30 * 1000);
            CTips_view.tick_to_close = true;
        end;
    end;

    RemoveTick = function()
        if CTips_view.tick_to_close == true then
            CTips_view.inner_log("RemoveTick");
            local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
            tpfui:RemoveInterval("CTips_view.TickCount");
            CTips_view.tick_to_close = false;
        end;
    end;

    TickCount = function()
        CTips_view.inner_log("TickCount");
        CTips_view.Close();
    end;

    OnJsCallback = function(uilib, fuc_name, json)
        CTips_view.inner_log("OnJsCallback : "..json);
        if fuc_name == "close_dlg" then
            CTips_view.Close();
        elseif fuc_name == "get_tips_info" then
            CTips_view.GetTipsInfo(json);
        elseif fuc_name == "set_title" then
            local param = base_utility.json_to_table(json);
            if nil ~= param and nil ~= param.title then
                CTips_view.SetTitle(param.title);
            end;
        end;
    end;

    GetTipsInfo = function(json)
        local param = base_utility.json_to_table(json);
        if nil ~= param and nil ~= param.data and nil ~= param.data.tipsid then
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            local info = CTips_control.tips_info_list[tonumber(param.data.tipsid)];
            if info == nil then
                info = {};
            end;
            rsp_table.data = {tips_info = info};   
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param.callback_func, rsp_json, CID_CTIPS_BROWSER);
            end
        end
    end;

    SetTitle = function(title)
        if CTips_view.Exists() then
            CTips_view.inner_log("SetTitle : "..title);
            ui_utility.setproperty_by_cid(CID_CTIPS_DLG, "Caption", title);
        end;
    end;

    OnTitleChanged = function(uilib, title, control_id)
        CTips_view.inner_log("OnTitleChanged : "..title);
        CTips_view.SetTitle(title);
    end;
    
    OnNavigateComplete = function()
        CTips_view.inner_log("OnNavigateComplete");
        CTips_view.Show(true);
        CTips_view.MoveToPosition();
        CTips_view.StartTick();
        CTips_control.ReportGameTipsShowed();
        CTips_view.busy = false;
    end;

    OnNavigateFailed = function()
        CTips_view.inner_log("OnNavigateFailed");
        CTips_view.Hide();
    end;

    OnCrash = function()
        CTips_view.inner_log("OnCrash");
        CTips_view.Hide();
    end;
    
    --多此一举的搞个弹出动画
    MoveToPosition = function ()
        local dlg_id = ui_utility.get_ctrl_id_by_cid(CID_CTIPS_DLG);
        if dlg_id == 0 then
            return false;
        end;
        local dlg_ptr = ui_utility.get_ctrl_ptr_by_id(dlg_id);
        if dlg_ptr == nil or dlg_ptr == 0 then
            return false;
        end;
        local tips_mgr = base_utility.get_component_adapter("ITIPS_mgr",
            "ierd_tgp.tips_mgr.GetTipsMgrModule();");
        if tips_mgr == nil then
            return false;
        end

        local resolution_x, resolution_y, work_area_x, work_area_y = 
            ui_utility.get_screen_resolution();

        local dlg_width, dlg_height = ui_utility.get_control_size_by_id(dlg_id);
        local start_pos = POINT:new_local();
        local end_pos = POINT:new_local();
        start_pos.x = work_area_x - dlg_width - 5;
        start_pos.y = work_area_y;
        end_pos.x = start_pos.x;
        end_pos.y = work_area_y - dlg_height - 5;

        animation_utility.init(dlg_id, "CTips_Move", animation_utility.ANI_TYPE.ANI_MOVE);
        animation_utility.move_animation(dlg_id, start_pos, end_pos, 15, 0);
    end;

    -- 关闭动画
    on_animation_start = function (uilib, control_id)
        CTips_view.inner_log("on_animation_start");
    end;

    on_animation_end = function (uilib, control_id)
        CTips_view.inner_log("on_animation_end");
    end;
};
CTips_view.init();